package com.bz.websocket;

import com.bz.websocket.utils.RandomKeyUtil;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 服务自身信息
 *
 * @author zhongyongbin
 * @since 2021/5/10
 */
public final class Self {

    private static final int ANONYMOUS_IDENTITY_LENGTH = 8;

    private static final Self instance = new Self();

    private final String name;

    private Self() {
        String name;
        try {
            name = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            name = "anonymous-" + RandomKeyUtil.generateKey(ANONYMOUS_IDENTITY_LENGTH);
        }

        this.name = name;
    }

    public static String getName() {
        return instance.name;
    }
}
